/*
 * @Author: c
 * @Date: 2023-12-02 05:38:11
 * @LastEditTime: 2024-01-05 22:31:53
 * @LastEditors: c
 * @Description: 用户名、密码 登录校验
 * @FilePath: \music-web-backend\src\auth\local.strategy.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';
import { UserNameNotFoundException } from 'src/common/exception/business.exception';
import { ApiEnum } from 'src/common/enums/api.enum';
import { EXCEPTION } from 'src/common/const/constants';

@Injectable()
export class UserLoginAuth  extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super();
  }

  async validate(username: string, password: string): Promise<any> {


    const user = await this.authService.validateUser(username, password);
    if (!user) {
      throw new UserNameNotFoundException(EXCEPTION.USER.NOT_FOUND);
    }
    return user;
  }
}